package com.glodon.paas.document.service.util;

import java.io.IOException;
import java.io.InputStream;

import com.glodon.paas.storage.BulkStorage;

public class WrapInputstream extends InputStream {

    private InputStream is;

    private int         count = 0;

    public WrapInputstream(InputStream is) {
        this.is = is;
    }

    @Override
    public int read() throws IOException {
        return is.read();
    }

    public int read(byte b[]) throws IOException {
        if (count >= BulkStorage.BLOCK_SIZE) {
            return -1;
        } else if (count + b.length > BulkStorage.BLOCK_SIZE) {
            b = new byte[(int) BulkStorage.BLOCK_SIZE - count];
        }
        int n = is.read(b);

        if (n != -1) {
            count += n;
        }
        return n;

    }

    public int read(byte b[], int off, int len) throws IOException {
        return is.read(b, off, len);
    }

    public void reset() throws IOException {
        is.reset();
    }

    public long skip(long n) throws IOException {
        return is.skip(n);
    }

    public int available() throws IOException {
        return is.available();
    }

    public void close() throws IOException {

    }

    public void closeForce() throws IOException {
        is.close();
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
